Type.registerNamespace("Telerik.Web.UI.Scheduler.Rendering");
(function(){var a=60000,c=$telerik.$,b=Telerik.Web.UI,d=b.Scheduler,e=Telerik.Web.UI.Scheduler.Rendering;
e.RenderingManager=function(f,g){this._owner=f;
this._settings=g;
this._schedulerTzOffset=this._owner._timeZoneOffset;
this._isWCFService=false;
this._appointmentsLoaded=false;
this._resourcesLoaded=false;
};
e.RenderingManager.prototype={initialize:function(){this._webServiceLoader=new b.WebServiceLoader(this._settings);
this._webServiceLoader.add_loadingSuccess(Function.createDelegate(this,this._onRequestSuccess));
this._webServiceLoader.add_loadingError(Function.createDelegate(this,this._onRequestError));
this._isWCFService=this._settings.get_path().endsWith("svc");
if(this._settings.get_resourcesPopulated()){this._resourcesLoaded=true;
}this.loadAppointments();
this.loadResources();
this._owner.add_propertyChanged(Function.createDelegate(this,this._onSchedulerPropertyChanged));
},get_events:function(){if(!this._events){this._events=new Sys.EventHandlerList();
}return this._events;
},loadAppointments:function(g){var h=this._getSchedulerInfo(g);
if(!this._onAppointmentsPopulating(h)){return;
}var f={schedulerInfo:h};
this._webServiceLoader.loadData(f,"loadAppointments");
},deleteAppointment:function(g,i){var k=this._getSchedulerInfo();
var j={appointment:g,editingRecurringSeries:i,schedulerInfo:k};
var h=c.raiseCancellableControlEvent(this._owner,"AppointmentWebServiceDeleting",j);
if(h){return;
}if(!this._onAppointmentsPopulating(k)){return;
}var f={schedulerInfo:k,appointmentData:this._storeAppointment(g),deleteSeries:i};
var l=this._settings.get_deleteAppointmentMethod();
this._webServiceLoader.invokeMethod(l,f,"deleteAppointment");
},insertAppointment:function(g){var i=this._getSchedulerInfo();
var k={appointment:g,schedulerInfo:i};
var j=c.raiseCancellableControlEvent(this._owner,"AppointmentWebServiceInserting",k);
if(j){return;
}if(!this._onAppointmentsPopulating(i)){return;
}var f={schedulerInfo:i,appointmentData:this._storeAppointment(g)};
var h=this._settings.get_insertAppointmentMethod();
this._webServiceLoader.invokeMethod(h,f,"insertAppointment");
g.set_allowDelete(false);
g.set_allowEdit(false);
},updateAppointment:function(f){if(f.__newRecurrenceException){this._createRecurrenceException(f);
}else{this._executeUpdate(f);
}},loadResources:function(){if(this._settings.get_resourcesPopulated()){return;
}var h=this._getSchedulerInfo();
var g=new b.SchedulerWebServiceEventArgs(h);
this._owner._raiseResourcesPopulating(g);
if(g.get_cancel()){return;
}this._owner.get_resources().clear();
var f={schedulerInfo:h};
var i=this._settings.get_getResourcesMethod();
this._webServiceLoader.invokeMethod(i,f,"loadResources");
},removeRecurrenceExceptions:function(h){var i=this._getSchedulerInfo();
var j={appointment:h,schedulerInfo:i};
var k=c.raiseCancellableControlEvent(this._owner,"RecurrenceExceptionsRemoving",j);
if(k){return;
}if(!this._onAppointmentsPopulating(i)){return;
}var f={schedulerInfo:i,masterAppointmentData:this._storeAppointment(h)};
var g=this._settings.get_removeRecurrenceExceptionsMethod();
this._webServiceLoader.invokeMethod(g,f,"removeRecurrenceExceptions");
},add_appointmentsReceived:function(f){this.get_events().addHandler("appointmentsReceived",f);
},add_appointmentsPopulated:function(f){this.get_events().addHandler("appointmentsPopulated",f);
},_getSchedulerInfo:function(f){var g;
if(f){g=this._owner._newActiveModel;
}else{g=this._owner.get_activeModel();
}return{ViewStart:this._toServerDate(g.get_visibleRangeStart()),ViewEnd:this._toServerDate(g.get_visibleRangeEnd()),EnableDescriptionField:this._owner._enableDescriptionField,MinutesPerRow:this._owner.get_minutesPerRow()};
},_executeUpdate:function(g){var j=this._getSchedulerInfo();
var k={appointment:g,schedulerInfo:j};
var i=c.raiseCancellableControlEvent(this._owner,"AppointmentWebServiceUpdating",k);
if(i){return;
}if(!this._onAppointmentsPopulating(j)){return;
}var f={schedulerInfo:j,appointmentData:this._storeAppointment(g)};
var h=this._settings.get_updateAppointmentMethod();
this._webServiceLoader.invokeMethod(h,f,"updateAppointment");
},_createRecurrenceException:function(g){var i=this._getSchedulerInfo();
var j={appointment:g,schedulerInfo:i};
var k=c.raiseCancellableControlEvent(this._owner,"RecurrenceExceptionCreating",j);
if(k){return;
}if(!this._onAppointmentsPopulating(i)){return;
}var f={schedulerInfo:i,recurrenceExceptionData:this._storeAppointment(g)};
var h=this._settings.get_createRecurrenceExceptionMethod();
this._webServiceLoader.invokeMethod(h,f,"createRecurrenceException");
},_onAppointmentsPopulating:function(g){var f=new b.SchedulerWebServiceEventArgs(g);
this._owner._raiseAppointmentsPopulating(f);
return !f.get_cancel();
},_onRequestSuccess:function(f,i){var g=i.get_data();
var h=g.Appointments||g;
this._owner._raiseRequestSuccess(new d.RequestSuccessEventArgs(g));
this._owner._suppressWebServiceCalls=true;
if(i.get_context()=="loadResources"){this._loadResources(this._owner.get_resources(),h);
this._resourcesLoaded=true;
this._owner._raiseResourcesPopulated();
}else{this._raiseEvent("appointmentsReceived");
this._populateAppointments(h);
this._raiseEvent("appointmentsPopulated");
this._appointmentsLoaded=true;
}if(this._appointmentsLoaded&&this._resourcesLoaded){this._owner._raiseDataBound();
}this._owner._suppressWebServiceCalls=false;
},_populateAppointments:function(f){var m=[];
for(var k=0,l=f.length;
k<l;
k++){var g=this._loadAppointment(f[k]);
Array.add(m,g);
var h=new d.AppointmentDataBoundEventArgs(g,f[k]);
this._owner._raiseAppointmentDataBound(h);
}m.sort(function(i,n){return i.compare(n);
});
var j=this._owner.get_appointments();
for(var k=0,l=m.length;
k<l;
k++){j.add(m[k]);
}this._owner.repaint();
},_onRequestError:function(f,i){var h=i.get_message();
var g=new b.SchedulerRequestFailedEventArgs(h);
this._owner._raiseRequestFailed(g);
if(!g.get_cancel()){alert(h);
}},_loadAppointment:function(f){var g=new b.SchedulerAppointment();
g._id=f.ID;
g._internalID=f.EncodedID;
g.set_subject(f.Subject);
g.set_description(f.Description);
g.set_start(this._toClientDate(f.Start));
g.set_end(this._toClientDate(f.End));
g._setOwner(this._owner);
g._recurrenceRule=f.RecurrenceRule;
g._recurrenceParentID=f.RecurrenceParentID;
g._recurrenceState=f.RecurrenceState;
g._visible=f.Visible;
g.set_toolTip(f.Subject);
if(f.Resources){this._loadResources(g.get_resources(),f.Resources);
}if(f.Attributes){this._loadAttributes(g.get_attributes(),f.Attributes);
}return g;
},_loadResources:function(h,k){for(var g=0,f=k.length;
g<f;
g++){var j=k[g];
var i=new b.SchedulerResource({key:j.Key,type:j.Type,text:j.Text,internalKey:j.EncodedKey,available:j.Available});
if(j.Attributes){this._loadAttributes(i.get_attributes(),j.Attributes);
}h.add(i);
}},_loadAttributes:function(g,h){if(this._isWCFService){for(var i=0;
i<h.length;
i++){var f=h[i];
g.setAttribute(f.Key,f.Value);
}}else{g._load(h);
}},_storeAppointment:function(f){var g={EncodedID:f._internalID,Subject:f.get_subject(),Description:f.get_description(),Start:this._toServerDate(f.get_start()),End:this._toServerDate(f.get_end()),RecurrenceRule:f._recurrenceRule,RecurrenceParentID:f._recurrenceParentID,RecurrenceState:f._recurrenceState};
var h=this._storeResources(f.get_resources());
if(h.length>0){g.Resources=h;
}g.Attributes=this._storeAttributes(f.get_attributes());
c.raiseControlEvent(this._owner,"appointmentSerialized",{appointment:f,data:g});
return g;
},_toClientDate:function(f){var g=new Date(f.getTime());
var h=g.getTimezoneOffset()*a;
g.setTime(g.getTime()+h);
g.setTime(g.getTime()+this._schedulerTzOffset);
return g;
},_toServerDate:function(f){var g=new Date(f.getTime());
g.setTime(g.getTime()-this._schedulerTzOffset);
var h=g.getTimezoneOffset()*a;
g.setTime(g.getTime()-h);
return g;
},_storeResources:function(h){var g=[];
var f=this;
h.forEach(function(i){Array.add(g,{Key:i.get_key(),Type:i.get_type(),Text:i.get_text(),EncodedKey:i._getInternalKey(),Available:i.get_available(),Attributes:f._storeAttributes(i.get_attributes())});
});
return g;
},_storeAttributes:function(g){if(this._isWCFService){return this._storeAttributesWCF(g);
}var f={};
g.forEach(function(i,h){f[i]=h;
});
return f;
},_storeAttributesWCF:function(g){var f=[];
g.forEach(function(h,i){Array.add(f,{Key:h,Value:i});
});
return f;
},_raiseEvent:function(g,h){var f=this.get_events().getHandler(g);
if(f){if(!h){h=Sys.EventArgs.Empty;
}f(this,h);
}},_onSchedulerPropertyChanged:function(f,g){switch(g.get_propertyName()){case"selectedDate":this.loadAppointments();
break;
case"selectedView":this.loadAppointments(true);
break;
}}};
e.RenderingManager.registerClass("Telerik.Web.UI.Scheduler.Rendering.RenderingManager");
})();
